package dcm4chee

import (
	"errors"
	"fmt"
	"strconv"
)

type DicomItem struct {
	Value []interface{} `json:"Value"`
	Vr    string        `json:"vr"`
}

func (d DicomItem) GetString() (r string, err error) {
	switch d.Vr {
	case "UI", "UR", "LO":
		return d.Value[0].(string), nil
	case "DA": //DATE [20201130]
		return d.Value[0].(string), nil
	case "TM": //TIME [093608]
		return d.Value[0].(string), nil
	case "PN": // [map[Alphabetic:HXY-LIZHE^18736011920]]
		if len(d.Value) == 0 {
			fmt.Println("no patient name")
			return "", nil
		}

		val := d.Value[0].(map[string]interface{})
		val1, ok := val["Alphabetic"]
		if ok {
			return val1.(string), nil
		} else {
			fmt.Println("E: decode patient name failed:", d.Value[0])
			return "", nil
		}

	default:
		fmt.Println(d.Vr, d.Value)
		return "", errors.New("cannot not decode")

	}
}

func (d DicomItem) GetInt() (r int, err error) {
	switch d.Vr {
	case "IS":
		rets := d.Value[0].(string)
		reti, err := strconv.Atoi(rets)
		return reti, err
	case "US":
		f := d.Value[0].(float64)
		return int(f), nil
	default:
		fmt.Println(d.Vr, d.Value)
		return 0, errors.New("cannot not decode")
	}
}
